﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataTool;
using DataTables;
using workflowMVC.Models;
using OneGateWF;
using Common;

namespace workflowMVC.Controllers
{
    [Authorize(Roles="Checker")]
    public class CheckController : Controller
    {
        //
        // GET: /Manage/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ConfirmStu()
        {
            ViewBag.datasource = Url.Action("ConfirmStuData");
            return View("ManageTable");            
        }

        public JsonResult ConfirmStuData()
        {
            var cs = Util.ValidateTaskListData(Request, x => x.Status == 1 && x.Kind == 1);
            cs.aaData.ForEach(
                x => Util.TableLinkMiscCheck(x,Url.Action("ConfirmStuDetail"))              
                    );
            return Json(cs);
        }

        public ActionResult ConfirmStuDetail(Guid Id)
        {
            if (new wfdbEntities().TASKs.FirstOrDefault(x => x.Id == Id && x.Status == 1) == null)
                return View("Error");
            SimpleTaskDetailModel m = new SimpleTaskDetailModel();
            try
            {
                m.Logs = Util.getLogsForWebPage(Id);
            }
            catch
            {
                return View("Error");
            }
            ViewBag.DisplayFormSourceAction = "getConfirmStuForm";
            ViewBag.DisplayFormSourceController = "TaskForm";
            return View("SimpleTaskDetail", m);
        }

        [HttpPost]
        public ActionResult ConfirmStuDetail(SimpleTaskDetailModel m,string bSubmit,Guid Id)
        {
            ValidateSession vs = new ValidateSession();
            switch (bSubmit)
            {
                case "Duyệt":
                    {
                        vs = Util.getValidateSession(m.Note, true, User.Identity.Name, Id);
                        ViewBag.NotifyResult = "Bạn đã duyệt thành công";
                        break;
                    }
                case "Từ chối":
                    {
                        vs = Util.getValidateSession(m.Note, false, User.Identity.Name, Id);
                        ViewBag.NotifyResult = "Bạn đã từ chối thành công";
                        break;
                    }
            }
            ValidateTaskHost h = new ValidateTaskHost();
            h.CheckTask(Id, vs);
            return View("NotifyResult");
        }

        public ActionResult ExcemptTuition()
        {
            ViewBag.datasource = Url.Action("ExcemptTuitionData");
            return View("ManageTable");
        }

        public JsonResult ExcemptTuitionData()
        {
            var cs = Util.ValidateTaskListData(Request, x => x.Status == 1 && x.Kind == 2);
            cs.aaData.ForEach(
                x => Util.TableLinkMiscCheck(x, Url.Action("ExcemptTuitionDetail"))
                    );
            
            return Json(cs);
        }

        public ActionResult ExcemptTuitionDetail(Guid Id)
        {
            if (new wfdbEntities().TASKs.FirstOrDefault(x => x.Id == Id && x.Status == 1) == null)
                return View("Error");
            ExcemptTuitionTaskRevModel m = new ExcemptTuitionTaskRevModel();
            try
            {
                m.Logs = Util.getLogsForWebPage(Id);
            }
            catch
            {
                return View("Error");
            }
            ViewBag.DisplayFormSourceAction = "getExcemptTuitionForm";
            ViewBag.DisplayFormSourceController = "TaskForm";
            return View(m);
        }

        [HttpPost]
        public ActionResult ExcemptTuitionDetail(Guid Id,ExcemptTuitionTaskRevModel m, string bSubmit)
        {
            ExcemptTuitionVS vs = new ExcemptTuitionVS();
            switch (bSubmit)
            {
                case "Duyệt":
                    {
                        vs = Util.getExcempTuitionVS(m, true, User.Identity.Name, Id);
                        ViewBag.NotifyResult = "Bạn đã duyệt thành công";
                        break;
                    }
                case "Từ chối":
                    {
                        vs = Util.getExcempTuitionVS(m, false, User.Identity.Name, Id);
                        ViewBag.NotifyResult = "Bạn đã từ chối thành công";
                        break;
                    }
            }
            ValidateTaskHost h = new ValidateTaskHost();
            h.CheckTask(Id, vs);
            return View("NotifyResult");
        }

        public ActionResult StudentCard()
        {
            ViewBag.datasource = Url.Action("StudentCardData");
            return View("ManageTable");
        }

        public JsonResult StudentCardData()
        {
            var cs = Util.ValidateTaskListData(Request, x => x.Status == 1 && x.Kind == 3);
            cs.aaData.ForEach(
                x =>Util.TableLinkMiscCheck(x,Url.Action("StudentCardDetail"))
                );
            return Json(cs);
        }

        public ActionResult StudentCardDetail(Guid Id)
        {
            if (new wfdbEntities().TASKs.FirstOrDefault(x => x.Id == Id && x.Status == 1) == null)
                return View("Error");
            SimpleTaskDetailModel m = new SimpleTaskDetailModel();
            try
            {
                m.Logs = Util.getLogsForWebPage(Id);
            }
            catch
            {
                return View("Error");
            }
            ViewBag.DisplayFormSourceAction = "getStudentCardForm";
            ViewBag.DisplayFormSourceController = "TaskForm";
            return View("SimpleTaskDetail", m);
        }

        [HttpPost]
        public ActionResult StudentCardDetail(SimpleTaskDetailModel m, string bSubmit, Guid Id)
        {
            ValidateSession vs = new ValidateSession();
            switch (bSubmit)
            {
                case "Duyệt":
                    {
                        vs = Util.getValidateSession(m.Note, true, User.Identity.Name, Id);
                        ViewBag.NotifyResult = "Bạn đã duyệt thành công";
                        break;
                    }
                case "Từ chối":
                    {
                        vs = Util.getValidateSession(m.Note, false, User.Identity.Name, Id);
                        ViewBag.NotifyResult = "Bạn đã từ chối thành công";
                        break;
                    }
            }
            ValidateTaskHost h = new ValidateTaskHost();
            h.CheckTask(Id, vs);
            return View("NotifyResult");
        }

        


        //[HttpPut]
        //public void Reject(Guid id)
        //{
        //    BusinessHost h = new BusinessHost();
        //    //h.CancelInstance(id);
        //}

        //[HttpPut]
        //public void Validate(Guid id)
        //{
        //    ValidateSession vs = new ValidateSession
        //    {
        //        IsAgree = true,
        //        Note = "abc",
        //        UserName = User.Identity.Name,
        //        UserRole = Util.GetRolesFromUser(User.Identity.Name),
        //        ValidTime = DateTime.Now
        //    };
        //    BusinessHost h = new BusinessHost();
        //    h.CheckTask(id,vs);
        //}

        
    }
}
